package com.echo.fm.testsms;

import java.io.IOException;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.echo.util.ResultUtils;
import com.echo.util.impl.JavaSmsApi;
import com.echo.util.impl.RegularJudge;
import com.echo.util.impl.ValidateUtils;

@Controller
public class ITestController {
	@Autowired
	@Qualifier("javaSmsApi")
	JavaSmsApi javaSmsApi;

	@RequestMapping(value="/web/sms/test")
	@ResponseBody
	public Object SmsApi(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String phone = request.getParameter("phone");
		if (ValidateUtils.isNotEmpty(phone)) {
			if(RegularJudge.isMobileNO(phone)){
				Random random = new Random();
				int value = random.nextInt(999999);
				String code = String.format("%06d", value);
				 String text = "【云片网】您的验证码是%s";
				 text = String.format(text, code);				 
				javaSmsApi.sendSms(text, phone);
			}
		}

		return ResultUtils.instJSONResult(ResultUtils.WEB_SUCCESS_CODE);

	}

}
